using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace temportal
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D link;

        GameInput mGameInput;
        public enum inputtypes
        {
            keyboard, controller1, controller2, controller3, controller4, noone
        }

        const string ActionExit = "Exit";
        const string ActionStart = "Start";
        const string ActionMoveUp = "MoveUp";
        const string ActionMoveRight = "MoveRight";
        const string ActionMoveDown = "MoveDown";
        const string ActionMoveLeft = "MoveLeft";

        List<Unit> Units = new List<Unit>();
        Dictionary<inputtypes, bool> activeplayers = new Dictionary<inputtypes, bool>();
        Vector4 tomove;

        SpriteFont namefont;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            // Indication that all possible input types are not active
            activeplayers.Add(inputtypes.keyboard, false);
            activeplayers.Add(inputtypes.controller1, false);
            activeplayers.Add(inputtypes.controller2, false);
            activeplayers.Add(inputtypes.controller3, false);
            activeplayers.Add(inputtypes.controller4, false);
            activeplayers.Add(inputtypes.noone, false);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            //p1 = new Unit();
            //p2 = new Unit();
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            namefont = Content.Load<SpriteFont>("Candara");
            link = Content.Load<Texture2D>("linkd1");

            mGameInput = new GameInput();
            initialButtons();

            // TODO: use this.Content to load your game content here
            Vector2 p1startposition = new Vector2(0.0f, 0.0f);
            string initp1name = "Joe";
            //p1.Initialize(link, p1startposition, Color.LightBlue, initp1name, namefont);

            Units.Add(new Unit(inputtypes.keyboard, link, new Vector2(0.0f), Color.LightBlue, initp1name, namefont));
            
        }

        void initialButtons()
        {
            // Exit
            mGameInput.AddGamePadInput(ActionExit, Buttons.Back, false);
            mGameInput.AddKeyboardInput(ActionExit, Keys.Escape, false);

            // Pause/Start
            mGameInput.AddGamePadInput(ActionStart, Buttons.Start, true);
            mGameInput.AddKeyboardInput(ActionStart, Keys.Enter, true);

            // Up!
            mGameInput.AddGamePadInput(ActionMoveUp, Buttons.LeftThumbstickUp, false);
            mGameInput.AddGamePadInput(ActionMoveUp, Buttons.DPadUp, false);
            mGameInput.AddKeyboardInput(ActionMoveUp, Keys.Up, false);
            mGameInput.AddKeyboardInput(ActionMoveUp, Keys.W, true);

            // Right!
            mGameInput.AddGamePadInput(ActionMoveRight, Buttons.LeftThumbstickRight, false);
            mGameInput.AddGamePadInput(ActionMoveRight, Buttons.DPadRight, false);
            mGameInput.AddKeyboardInput(ActionMoveRight, Keys.Right, false);
            mGameInput.AddKeyboardInput(ActionMoveRight, Keys.D, true);

            // Down!
            mGameInput.AddGamePadInput(ActionMoveDown, Buttons.LeftThumbstickDown, false);
            mGameInput.AddGamePadInput(ActionMoveDown, Buttons.DPadDown, false);
            mGameInput.AddKeyboardInput(ActionMoveDown, Keys.Down, false);
            mGameInput.AddKeyboardInput(ActionMoveDown, Keys.S, true);

            // Left!
            mGameInput.AddGamePadInput(ActionMoveLeft, Buttons.LeftThumbstickLeft, false);
            mGameInput.AddGamePadInput(ActionMoveLeft, Buttons.DPadLeft, false);
            mGameInput.AddKeyboardInput(ActionMoveLeft, Keys.Left, false);
            mGameInput.AddKeyboardInput(ActionMoveLeft, Keys.A, true);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            mGameInput.BeginUpdate();

            // Check for the start button being pressed
            
            // Check for new players here




            for (PlayerIndex i=PlayerIndex.One; i<=PlayerIndex.Four; i++)
            {
                // Allows the game to exit
                if (mGameInput.IsPressed(ActionExit, i))
                    this.Exit();

                if (mGameInput.IsPressed(ActionStart, i))
                {



                }




            }            

            // Lazy player move, will need to migragte to a move class
            //  Should also foreach this for each "active" player

            if (mGameInput.IsPressed(ActionMoveUp, PlayerIndex.One))
                tomove.X = 1;
            else
                tomove.X = 0;
                //p1.position.Y -= p1.fullSpeed;
            if (mGameInput.IsPressed(ActionMoveRight, PlayerIndex.One))
                tomove.Y = 1;
            else
                tomove.Y = 0;
                //p1.position.X += p1.fullSpeed;
            if (mGameInput.IsPressed(ActionMoveDown, PlayerIndex.One))
                tomove.Z = 1;
            else
                tomove.Z = 0;
                //p1.position.Y += p1.fullSpeed;
            if (mGameInput.IsPressed(ActionMoveLeft, PlayerIndex.One))
                tomove.W = 1;
            else
                tomove.W = 0;
                //p1.position.X -= p1.fullSpeed;

            mGameInput.EndUpdate();
            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            // Draw the sprite.
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            foreach (Unit guy in Units)
                guy.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}